package com.doris.lucene;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

/**
 * 应用配置文件
 * 
 * @author root
 *
 */
public class LuceneProperties {

	private static final Logger LOGGER = Logger.getLogger(LuceneProperties.class);
	/**
	 * 配置文件中的索引目录条目名称
	 */
	private static final String SEARCH_DIR = "serach_dir";
	/**
	 * 索引存放目录
	 */
	private static String indexDir;
	/**
	 * 锁定目录
	 */
	private static String lockedDir;
	/**
	 * 线程池初始大小
	 */
	private static int corePoolSize;
	/**
	 * 线程池最大容量
	 */
	private static int maximumPoolSize;
	/**
	 * 索引爬取目录
	 */
	private static List<Object> searchDirs = new ArrayList<Object>();
	/**
	 * 允许索引的文件格式
	 */
	private static List<Object> suffixs = new ArrayList<Object>();
	
	private LuceneProperties() {
	}

	static {
		try {
			PropertiesConfiguration prop = new PropertiesConfiguration(Constants.LUCENE_PROPERTIES);
			indexDir = prop.getString("index_dir").trim();
			corePoolSize = prop.getInt("core_pool_size");
			maximumPoolSize = prop.getInt("maximum_pool_size");
			lockedDir = Constants.DEFAULT_LOCKED_DIR;
			searchDirs = prop.getList(SEARCH_DIR);
			suffixs = prop.getList("support_suffix");
		} catch (ConfigurationException e) {
			LOGGER.error("Lucene配置读取失败", e);
		}
	}

	/**
	 * 保存配置文件
	 * 
	 * @throws ConfigurationException
	 */
	public static void save() throws ConfigurationException {
		PropertiesConfiguration prop = new PropertiesConfiguration(Constants.LUCENE_PROPERTIES);
		prop.setEncoding(Constants.UTF8);
		prop.setProperty(SEARCH_DIR, LuceneProperties.getSearchDirs());
		prop.save();
	}

	public static synchronized String getLockedDir() {
		return lockedDir;
	}

	public static synchronized void setLockedDir(String lockedDir) {
		LuceneProperties.lockedDir = lockedDir;
	}

	public static synchronized List<Object> getSearchDirs() {
		return searchDirs;
	}

	public static synchronized void setSearchDirs(List<Object> searchDirs) {
		LuceneProperties.searchDirs = searchDirs;
	}

	public static String getIndexDir() {
		return indexDir;
	}

	public static int getCorePoolSize() {
		return corePoolSize;
	}

	public static int getMaximumPoolSize() {
		return maximumPoolSize;
	}

	public static List<Object> getSuffixs() {
		return suffixs;
	}

	public static void main(String[] args) {
		LOGGER.info(LuceneProperties.getSearchDirs());
		LOGGER.info(LuceneProperties.getSuffixs());
	}

}
